<template>
    <div class="pet-management">
      <h2>宠物信息</h2>

      <!-- 宠物信息表格 -->
      <table>
        <thead>
          <tr>
            <th @click="sortPets('petname')">宠物名 <span v-if="sortField === 'petname'">{{ sortOrder === 'asc' ? '↑' : '↓' }}</span></th>
            <th @click="sortPets('pettype')">宠物类型 <span v-if="sortField === 'pettype'">{{ sortOrder === 'asc' ? '↑' : '↓' }}</span></th>
            <th @click="sortPets('breed')">宠物品种 <span v-if="sortField === 'breed'">{{ sortOrder === 'asc' ? '↑' : '↓' }}</span></th>
            <th @click="sortPets('age')">宠物年龄 <span v-if="sortField === 'age'">{{ sortOrder === 'asc' ? '↑' : '↓' }}</span></th>
            <th @click="sortPets('location')">宠物所在地 <span v-if="sortField === 'location'">{{ sortOrder === 'asc' ? '↑' : '↓' }}</span></th>
            <th @click="sortPets('adopted')">是否被领养 <span v-if="sortField === 'adopted'">{{ sortOrder === 'asc' ? '↑' : '↓' }}</span></th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="pet in filteredPets" :key="pet.id">
            <td>{{ pet.petname }}</td>
            <td>{{ pet.pettype }}</td>
            <td>{{ pet.breed }}</td>
            <td>{{ pet.age }}</td>
            <td>{{ pet.location }}</td>
            <td>{{ pet.adopted ? '是' : '否' }}</td>
            <td>
              <button @click="showDetails(pet)">详情</button>
              <button @click="deletePet(pet.id)">删除</button>
            </td>
          </tr>
        </tbody>
      </table>
      <!-- 显示宠物详细信息的模态框 -->
      <div v-if="selectedPet" class="modal">
        <div class="modal-content">
          <span class="close" @click="closeModal">&times;</span>
          <h3>宠物详细信息</h3>
          <div class="pet-details">
            <p><strong>宠物名：</strong>{{ selectedPet.petname }}</p>
            <p><strong>宠物类型：</strong>{{ selectedPet.pettype }}</p>
            <p><strong>宠物品种：</strong>{{ selectedPet.breed }}</p>
            <p><strong>宠物年龄：</strong>{{ selectedPet.age }}</p>
            <p><strong>描述：</strong>{{ selectedPet.petdescription }}</p>
            <p><strong>是否接种疫苗：</strong>{{ selectedPet.vaccinated ? '是' : '否' }}</p>
            <p><strong>疫苗名称：</strong>{{ selectedPet.vaccine || '无' }}</p>
            <p><strong>宠物所在地：</strong>{{ selectedPet.location }}</p>
            <p><strong>发布人姓名：</strong>{{ selectedPet.ownername }}</p>
            <p><strong>联系方式：</strong>{{ selectedPet.contact }}</p>
            <p><strong>是否领养：</strong>{{ selectedPet.adopted==null ? '是' : '否' }}</p>
            <p><strong>领养人姓名：</strong>{{ selectedPet.adopted==null  ? selectedPet.adopterName : '无' }}</p>
          </div>
        </div>
      </div>
      <div v-if="deletedPetMessage" class="message">{{ deletedPetMessage }}</div>
      <div v-if="filteredPets.length === 0">
        <p>目前没有符合条件的宠物信息。</p>
      </div>
    </div>
</template>
  
<script>
  import { findAllPet } from '../../api/admins.js';
  export default {
    data() {
      return {
        pets: [

        ],
        searchQuery: '',
        filteredPets: [],
        sortField: '',
        sortOrder: 'asc',
        selectedPet: null, 
        deletedPetMessage:''
      };
    },
    mounted() {

        this.fetchPets();

    },
    methods: {
      async fetchPets() {
        try {
          const response = await findAllPet();
          this.pets = response.data.data;
          console.log( this.pets );
          this.filteredPets = response.data.data;
        } catch (error) {
          console.error('Error fetching pets:', error);
        }
      },
      filterPets() {
        this.filteredPets = this.pets.filter(pet => 
          pet.petname.includes(this.searchQuery) ||
          pet.pettype.includes(this.searchQuery) ||
          pet.breed.includes(this.searchQuery) ||
          pet.location.includes(this.searchQuery)
        );
      },
      sortPets(field) {
        if (this.sortField === field) {
          this.sortOrder = this.sortOrder === 'asc' ? 'desc' : 'asc';
        } else {
          this.sortField = field;
          this.sortOrder = 'asc';
        }
        this.filteredPets.sort((a, b) => {
          const aValue = a[field];
          const bValue = b[field];
          if (aValue > bValue) return this.sortOrder === 'asc' ? 1 : -1;
          if (aValue < bValue) return this.sortOrder === 'asc' ? -1 : 1;
          return 0;
        });
      },
      startAdding() {
        this.isAdding = true;
      },
      cancelAdding() {
        this.isAdding = false;
        this.newPet = { petname: '', pettype: '', breed: '', age: 0, location: '', adopted: false }; 
      },
      showDetails(pet) {
        this.selectedPet = pet;
      },
      closeModal() {
        this.selectedPet = null;
      },
      //挂载

      deletePet(petId) {
        if (confirm("确定要删除这只宠物吗？")) {
          this.pets = this.pets.filter(pet => pet.id !== petId);
          this.filterPets();
        }
        this.deletedPetMessage = `宠物已删除！`;
        // 2秒后清除提示信息
        setTimeout(() => {
          this.deletedPetMessage = '';
        }, 2000);
      },
    },
  };
</script>
  
<style scoped>
.pet-management {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px;
  font-family: 'Arial', sans-serif;
  background-color: #f7f7f7;
}
h2 {
    font-size: 24px;
    color: #333;
    text-align: center;
    margin-bottom: 20px;
}

.search-container {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}
.search-container input {
  padding: 10px;
  width: 300px;
  font-size: 1rem;
  border: 1px solid #ddd;
  border-radius: 5px;
  transition: border-color 0.3s ease;
}

.search-container input:focus {
  border-color: #5b9bd5;
  outline: none;
}
  
table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}  
th, td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #ddd;
}
th {
  background-color: #f4f4f4;
  cursor: pointer;
  font-weight: bold;
  position: relative;
}
th:hover {
    background-color: #e2e2e2;
}
tr:hover {
    background-color: #fafafa;
}
th span {
  position: absolute;
  right: 10px;
  font-size: 14px;
}
 
button {
  padding: 6px 12px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s ease;
  margin-right: 10px;
}
button:hover {
  background-color: #45a049;
}
button:disabled {
  background-color: #dcdcdc;
  cursor: not-allowed;
}
  
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
}
.modal-content {
    background-color: #fff;
    padding: 20px;
    border-radius: 8px;
    width: 500px;
    max-width: 90%;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    position: relative;
}
.modal h3 {
  text-align: center;;
}
  
.close {
    position: absolute;
    top: 5px;
    right: 10px;
    font-size: 30px;
    cursor: pointer;
    color: #333;
}
  
.pet-details p {
    margin: 8px 0;
    text-align: left;
}
.message {
  color: green;
  font-size: 1rem;
  text-align: center;
  margin-top: 20px;
}
</style>